package catering

import (
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/catering"
	cateringReq "gitcode.com/G-YT/ANN/server/model/catering/request"
	"gitcode.com/G-YT/ANN/server/model/common/request"
	"gitcode.com/G-YT/ANN/server/model/common/response"
	"gitcode.com/G-YT/ANN/server/service"
	"gitcode.com/G-YT/ANN/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type FoodApi struct {
}

var foodService = service.ServiceGroupApp.CateringServiceGroup.FoodService

// CreateFood 創建配餐
// @Tags Food
// @Summary 創建配餐
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body catering.Food true "創建配餐"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"創建成功"}"
// @Router /food/createFood [post]
func (foodApi *FoodApi) CreateFood(c *gin.Context) {
	var food catering.Food
	err := c.ShouldBindJSON(&food)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := foodService.CreateFood(&food); err != nil {
		global.GVA_LOG.Error("創建失敗!", zap.Error(err))
		response.FailWithMessage("創建失敗", c)
	} else {
		response.OkWithMessage("創建成功", c)
	}
}

// DeleteFood 删除配餐
// @Tags Food
// @Summary 删除配餐
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body catering.Food true "删除配餐"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /food/deleteFood [delete]
func (foodApi *FoodApi) DeleteFood(c *gin.Context) {
	var food catering.Food
	err := c.ShouldBindJSON(&food)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := foodService.DeleteFood(food); err != nil {
		global.GVA_LOG.Error("删除失敗!", zap.Error(err))
		response.FailWithMessage("删除失敗", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteFoodByIds 批量删除配餐
// @Tags Food
// @Summary 批量删除配餐
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除配餐"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /food/deleteFoodByIds [delete]
func (foodApi *FoodApi) DeleteFoodByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := foodService.DeleteFoodByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失敗!", zap.Error(err))
		response.FailWithMessage("批量删除失敗", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateFood 更新配餐
// @Tags Food
// @Summary 更新配餐
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body catering.Food true "更新配餐"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /food/updateFood [put]
func (foodApi *FoodApi) UpdateFood(c *gin.Context) {
	var food catering.Food
	err := c.ShouldBindJSON(&food)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)
	if err := foodService.UpdateFood(food, userID); err != nil {
		global.GVA_LOG.Error("更新失敗!", zap.Error(err))
		response.FailWithMessage("保存失敗", c)
	} else {
		response.Result(200, "", "保存成功", c)
	}
}

// FindFood 用id査詢配餐
// @Tags Food
// @Summary 用id査詢配餐
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query catering.Food true "用id査詢配餐"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"査詢成功"}"
// @Router /food/findFood [get]
func (foodApi *FoodApi) FindFood(c *gin.Context) {
	var food catering.Food
	err := c.ShouldBindQuery(&food)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)
	if refood, err := foodService.GetFood(userID); err != nil {
		global.GVA_LOG.Error("査詢失敗!", zap.Error(err))
		response.FailWithMessage("査詢失敗", c)
	} else {
		response.OkWithData(gin.H{"refood": refood}, c)
	}
}

// GetFoodList 分页獲取配餐列表
// @Tags Food
// @Summary 分页獲取配餐列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query cateringReq.FoodSearch true "分页獲取配餐列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /food/getFoodList [get]
func (foodApi *FoodApi) GetFoodList(c *gin.Context) {
	var pageInfo cateringReq.FoodSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := foodService.GetFoodInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "獲取成功", c)
	}
}

// GetLastPlanList 獲取最近方案列表
// @Tags Food
// @Summary 獲取最近方案列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query cateringReq.FoodSearch true "獲取最近方案列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /food/getLastPlanList [get]
func (foodApi *FoodApi) GetLastPlanList(c *gin.Context) {
	userID := c.Query("userID")
	if result, err := foodService.GetLastPlanList(userID); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(result, "獲取成功", c)
	}
}
